package com.haiyou.common.http;

import java.io.PrintWriter;

import com.haiyou.common.event.AbstractEvent;
import com.haiyou.common.event.asyc.AsyncEventBuses;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



/**
 * 
 * 
 * @Description: mvc 抽象层消息封装
 * @author xingyuan
 * @date 2022年3月9日 下午3:13:39
 */
public class AbstractController {

	/**
	 * json 格式
	 */
	public static final String JSON_CONTENT_TYPE = "application/json;charset=UTF-8";

	
	/**
	 * 获取 api Id
	 * @param httpRequest
	 * @return
	 */
	public int api(HttpServletRequest httpRequest) {
		return (Integer)httpRequest.getAttribute(HttpConstants.header_modul_attr_name);
	}
	
	/**
	 * 获取 token
	 * @param httpRequest
	 * @return
	 */
	public String token(HttpServletRequest httpRequest) {
		return String.valueOf(httpRequest.getAttribute(HttpConstants.header_token_attr_name));
	}
	
	
	/**
	 * 成功消息体
	 * @param code
	 * @param msg
	 * @param object
	 */
	public HttpResult successResult(String msg,Object object) {
		return HttpResultUtils.successResult(msg, object);
	}
	
	/**
	 * 成功消息体构建
	 * @param object
	 */
	public HttpResult successResult(Object object) {
		return HttpResultUtils.successResult("", object);
	}
	
	public HttpResult successResult() {
		return HttpResultUtils.successResult("", null);
	}
	
	/**
	 * 失败消息体
	 * @param code
	 * @param msg
	 * @return
	 */
	public HttpResult failResult(int code,String msg) {
		return HttpResultUtils.failResult(code, msg);
	}
	
	protected String getRequestParam(HttpServletRequest request, String key) {
		return request.getParameterMap().containsKey(key) ? request.getParameter(key) : "";
	}
	

	/**
	 * 推送事件
	 * @param event
	 */
	public void postEvent(AbstractEvent event) {
		AsyncEventBuses.getInstance().postEvent(event);
	}
	

}

